<?php
	class Follow extends CWidget {
		public	$assetPath		= 'widgets.like.assets';
		public	$assetUrl;
		public	$objectId		= null;
		public	$ratingTemplate	= 'greennet.modules.ratings.views.rating-template';
		public	$type			= 'follow';
		public	$text			= true;
		
		public	$classLike		= null;
		public	$classUnlike	= null;
		
		public	$actionLike		= null;
		public	$actionUnlike	= null;

		
		public	$self = false;

		

		
		public function init() {

		}
		
		public function run() {
			
			$modelObject	= new Follows;
			
			$arr			= array(
				'object_id'		=> $this->objectId,
				'type'			=> $this->type,
			);
			// dump($modelObject);
			$statistic = $modelObject::model()->getLikes($this->objectId);
			// dump($statistic);
			if(!empty($statistic)){
				if(currentUser() == false){
					$result			= array(
							'classRating'	=> '',
							'action'		=> $this->actionLike,
							'value'	=> 'Unlike',
							'count'		=> count($statistic),
						);
				}else{
					$this->self = $modelObject::model()->selfInLike(currentUser()->id,$this->objectId);
					
					// $numberPeople	= $modelObject::model()->peopleRate($statistic->count, $this->self);
					if ($this->self) {
						$result			= array(
							'classRating'	=> '',
							'action'		=> $this->actionUnlike,
							'value'	=> 'Unfollow',
							'count'		=> count($statistic),
						);
					} else {
						$result			= array(
							'classRating'	=> '',
							'action'		=> $this->actionLike,
							'value'	=> 'Unfollow',
							'count'		=> count($statistic),
						);
					}
				}
			}else{
				$result			= array(
					'classRating'	=> '',
					'value'	=> 'Follow',
					'action'		=> $this->actionLike,
					'count'		=> 0,
				);
			}
			
			
			$result	= array_merge($arr, $result);
			$strToken = md5(uniqid(32));
			$this->render('follow', compact('result','strToken'));
		}
		public function outString($countLike=null)
		{
			if ($countLike != 0) {
				if (!$this->self) {
					return ' <a href="javascript:void(0)"> '.$countLike.' people </a> follow this.';
				} else {
					$countLike	-= 1;
					if ($countLike>0) {
						return ' You and <a href="javascript:void(0)">'.$countLike.' people </a> follow this.';					
					} else {
						return ' You follow this';
					}
				}
			} else {
				return 'Follow';
			}
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	